package com.bgamq.springboot.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class JedisUtil {

   /* @Autowired(required = false)
    private JedisPool jedisPool;*/

    @Autowired
    private RedisTemplate redisTemplate;

   /* @Autowired
    private JedisConfig jedisConfig;*/

   /* private Jedis getJedis() {
        return jedisConfig.redisPoolFactory().getResource();
    }*/

    /**
     * 设值
     *
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
       // Jedis jedis = null;
        try {
          //  jedis = getJedis();
            //  return jedis.set(key, value);
            ValueOperations operations = redisTemplate.opsForValue();
            operations.set(key,value);
            return "";
        } catch (Exception e) {
            log.error("set key:{} value:{} error", key, value, e);
            return null;
        } finally {
           // close(jedis);
        }
    }

    /**
     * 设值
     *
     * @param key
     * @param value
     * @param expireTime 过期时间, 单位: s
     * @return
     */
    public String set(String key, String value, Long expireTime) {
      //  Jedis jedis = null;
        try {
            /*jedis = getJedis();
            return jedis.setex(key, expireTime, value);*/

            ValueOperations operations = redisTemplate.opsForValue();
            operations.set(key,value);
            Boolean bool = redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            if (bool)
                return Constant.Redis.OK;
            else
                return null;
        } catch (Exception e) {
            log.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e);
            return null;
        } finally {
          //  close(jedis);
        }

    }

    /**
     * 取值
     *
     * @param key
     * @return
     */
    public String get(String key) {
       // Jedis jedis = null;
        try {
            /*jedis = getJedis();
            return jedis.get(key);*/

            ValueOperations operations = redisTemplate.opsForValue();
            return (String) operations.get(key);

        } catch (Exception e) {
            log.error("get key:{} error", key, e);
            return null;
        } finally {
          //  close(jedis);
        }
    }

    /**
     * 删除key
     *
     * @param key
     * @return
     */
    public Boolean del(String key) {
       // Jedis jedis = null;
        try {
           /* jedis = getJedis();
            return jedis.del(key.getBytes());*/
          return   redisTemplate.delete(key);
        } catch (Exception e) {
            log.error("del key:{} error", key, e);
            return null;
        } finally {
           // close(jedis);
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
       // Jedis jedis = null;
        try {
            //jedis = getJedis();
            //return jedis.exists(key.getBytes());
            ValueOperations operations = redisTemplate.opsForValue();
            if(Objects.nonNull(operations.get(key))){
                return  true;
            }
        } catch (Exception e) {
            log.error("exists key:{} error", key, e);
            return null;
        } finally {
          //  close(jedis);
        }
        return false;
    }

   /* *//**
     * 设值key过期时间
     *
     * @param key
     * @param expireTime 过期时间, 单位: s
     * @return
     *//*
    public Long expire(String key, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.expire(key.getBytes(), expireTime);
        } catch (Exception e) {
            log.error("expire key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }*/

   /* *//**
     * 获取剩余时间
     *
     * @param key
     * @return
     *//*
    public Long ttl(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.ttl(key);
        } catch (Exception e) {
            log.error("ttl key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }*/

    /*private void close(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }*/
}

